package com.zr.controller.sys;

import com.github.pagehelper.PageInfo;
import com.zr.service.sys.SysUserService;
import com.zr.utils.AjaxResult;
import com.zr.utils.RedisUtil;
import com.zr.vo.LoginUser;
import com.zr.vo.sys.SysUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户逻辑处理
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 用户登录
     * @param sysUser
     * @return
     * @author
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody SysUser sysUser){
        SysUser loginSysUser = sysUserService.login(sysUser);
        AjaxResult ajaxResult = null;
        if(loginSysUser != null) {
            //设置返回的Map对象
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("loginUserName",loginSysUser.getUserName());
            objectMap.put("headImgUrl",sysUserService.getHeadImgUrl(loginSysUser.getImgUrl()));
            //自定义认证使用的token
            String token =  UUID.randomUUID().toString();
            objectMap.put("token", token);
            //token存入redis并设置key过期时间
            redisUtil.set(token, new LoginUser(loginSysUser), 700L, TimeUnit.MINUTES);
            ajaxResult = AjaxResult.success(200,"登录成功", objectMap);
        }else{
            ajaxResult = AjaxResult.fail(200,"账号或密码错误", null);
        }
         return ajaxResult;
    }

    /**
     * 用户退出
     * @return
     */
    @RequestMapping("/logout")
    public AjaxResult logout(){
        sysUserService.logout();
        return AjaxResult.success(200,"退出成功", null);
    }

    /**
     * 条件查询用户列表
     * @param sysUser
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult list(@RequestBody SysUser sysUser){
        PageInfo<SysUser> pageInfo = sysUserService.list(sysUser);
        return AjaxResult.success(200, "查询成功", pageInfo);
    }

    /**
     * 新增用户
     * @param sysUser
     * @return
     */
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody SysUser sysUser){
        sysUserService.add(sysUser);
        return AjaxResult.success(200, "新增成功", null);
    }

    /**
     * 编辑用户
     * @param sysUser
     * @return
     */
    @RequestMapping("/edit")
    public AjaxResult edit(@RequestBody SysUser sysUser){
        sysUserService.edit(sysUser);
        return AjaxResult.success(200, "修改成功", null);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id){
        sysUserService.remove(id);
        return AjaxResult.success(200, "删除成功", null);
    }

    /**
     * 更改用户状态
     * @param sysUser
     * @return
     */
    @RequestMapping("/change")
    public AjaxResult change(@RequestBody SysUser sysUser){
        sysUserService.change(sysUser);
        return AjaxResult.success(200, "操作成功", null);
    }

    /**
     * 设置用户头像
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/setHeadImg")
    public AjaxResult setHeadImg(MultipartFile file) throws IOException {
        return AjaxResult.success(200,"上传成功",sysUserService.setHeadImg(file));
    }

    /**
     * 查询人员信息
     * @return
     */
    @PostMapping("/getPersonInfo")
    public AjaxResult getPersonInfo(){
        return AjaxResult.success(200,"查询成功",sysUserService.getPersonInfo());
    }

}
